package ocg.common.re.influx.Influx;


import ocg.common.re.influx.GatlingParser.HttpRequest;
import ocg.common.re.influx.GatlingParser.HttpUser;
import ocg.common.re.influx.GatlingParser.SimulationContext;
import org.influxdb.InfluxDB;
import org.influxdb.dto.Point;
import org.influxdb.dto.Point.Builder;

import java.util.List;
import java.util.concurrent.TimeUnit;


/**
 * Created by chenbo on 2018/2/11.
 * 测试数据 SimulationContext 写入 influxDB
 */
public class GatlingInflux extends InfluxService {

    int responses = 0;
    int error = 0;

    public GatlingInflux( InfluxDB influxDB ) {
        super ( influxDB );
    }

    /**
     * 将 users 和 requests 分类，通过不同的方法写入influxDB
     * @param context
     */
    public void httpDate(SimulationContext context ){

        createDatabase ( "gatling" );

        String siumlation = context.getSimulationName ();
        long start = context.getSimulationStartTime ();

        List<HttpUser> users = context.getUsers ();

        for ( int i = 0 ; i < users.size () ; i ++ ){
            insertUser( siumlation , users.get ( i ));
        }

        List<HttpRequest> requests = context.getRequests ();

        for ( int i = 0 ; i < requests.size () ; i ++ ){
            insertRequest( siumlation , requests.get ( i ));

        }
    }

    /**
     * 写入 users 部分数据
     * @param simulation
     * @param user
     */
    public void insertUser( String simulation , HttpUser user ){
        Builder USER = Point.measurement ( "USER" );
            USER.tag ( "Simulation_Name" , simulation )
                .tag ( "Scenario_Name" , user.getScenario () )
                .addField ( "User_ID" , user.getUser_ID () )
                .tag ( "User_Type" , user.getUserType () );

        long time = user.getTime ();

        USER.time ( time , TimeUnit.MILLISECONDS );

        insert ( "gatling" , USER );
    }

    /**
     * 写入 requests 部分的数据
     * @param simulation
     * @param request
     */
    public void insertRequest( String simulation , HttpRequest request ){
        responses++;
        Builder REQUEST = Point.measurement ( "REQUEST" );



        REQUEST.tag ( "Simulation_Name" , simulation )
                .tag ( "Scenario_Name" , request.getScenario () )
                .tag ( "User_ID" , String.valueOf ( request.getUser_ID () ) )
                    .tag ( "Request_Name" , request.getHttp () )
                .addField("request" , responses );

        REQUEST.time ( request.getStartTime () , TimeUnit.MILLISECONDS );

        insert ( "gatling" , REQUEST );

        Builder RESPONSE = Point.measurement ( "RESPONSES" );

        RESPONSE.tag ( "Simulation_Name" , simulation )
                .tag ( "Scenario_Name" , request.getScenario () )
                .tag ( "User_ID" , String.valueOf ( request.getUser_ID () ) )
                .addField ( "Request_Name" , request.getHttp () )
                .addField ( "Response_Time" , request.getResponseTime () )
                .tag ( "Success" , request.getSuccess () )
                .addField("response" , responses);


        if ( request.getSuccess ().equals ( "KO" ) ) {
            error++;
            RESPONSE.tag ( "Error" , request.getHttp () + " : " + request.getError () );
            RESPONSE.addField ( "errorCount" , error );
        }else {
            RESPONSE.tag ( "Error" , "-" );
            RESPONSE.addField ( "errorCount" , 0 );
        }

        RESPONSE.time ( request.getEndTime () , TimeUnit.MILLISECONDS );


        if ( ! insert ( "gatling" , RESPONSE ) ) {
            System.out.println ( responses );
        }

    }

}
